<?php
class Category_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }

    
    /**
     *  获得类别
     * @return array
     */
    function get_cat()
    {
    	$cate_data = array();
    	$query = $this->db->query('SELECT * FROM '.$this->db->dbprefix('category'));
    	foreach ($query->result_array() as $row)
    	{
			$cate_data[$row['cat_id']] = $row; 
    	}
    	$cate_tree = array();
    	foreach($cate_data as $id=>$cate)
    	{
    		if($cate['parent_id'] != 0)
    			$cate_tree[$cate['parent_id']]['c'][] = $cate;
    		else
    			$cate_tree[$id] = $cate; 
    	}    	
    	return $cate_tree;
    }
    
    function get_children_cat($cat_id)
    {
    	$cat_id = intval($cat_id);
    	$query = $this->db->query('SELECT * FROM '.$this->db->dbprefix('category').' where parent_id = '.$cat_id);
    	return $query->result_array();
    } 

    function get_cat_one($cat_id)
    {
    	$cat_id = intval($cat_id);
    	$query = $this->db->query('SELECT * FROM '.$this->db->dbprefix('category').' where cat_id = '.$cat_id);
    	return $query->first_row('array');
    }
}